package com.xiaohao.communication.daoImpl;

import com.xiaohao.communication.bean.Collection;
import com.xiaohao.communication.dao.UserCollectionDao;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.mongodb.core.MongoTemplate;
import org.springframework.data.mongodb.core.query.Criteria;
import org.springframework.data.mongodb.core.query.Query;
import org.springframework.data.mongodb.core.query.Update;
import org.springframework.stereotype.Repository;

import java.util.List;


@Repository
public class UserCollectionDaoImpl implements UserCollectionDao {

    @Autowired
    MongoTemplate mongoTemplate;

    //获取收藏
    @Override
    public List<Collection> getCollection(String username ) {
        List<Collection> collections = mongoTemplate.find(new Query(Criteria.where("username").is(username)), Collection.class, "collection");
        return collections;
    }

    //增加收藏
    @Override
    public void addCollection(Collection collection) {
        mongoTemplate.save(collection);
    }


    /*
    *  删除收藏的话题
    *
    *  对于删除，前端只传送话题id，改变状态码为0；  1为存在，0为删除
    * */
    @Override
    public void deleteCollection(int topic_id) {

        Query query = new Query(Criteria.where("topic_id").is(topic_id));

        Update update = new Update();
        update.set("collectionState",0);

        mongoTemplate.updateFirst(query,update,Collection.class,"collection");


    }


}
